require("Entity")
require("Nations")
require("Player")
require("Bullet")
require("Button")

GameScene = {}

function GameScene:new()
    local o = {}
    setmetatable(o, {__index = GameScene})
    o:initialize()
    return o
end

function GameScene:initialize()
    self.entities = {
        Nazis = {},
        American = {},
        Bullets = {},
        Buttons = {}
    }
    self.soldiers = {
        Nazis = self.entities["Nazis"],
        American = self.entities["American"]
    }
    self.players = {}
    self.win = false
    self.winDt = 0

    for nation, props in pairs(Nations) do
        self.players[nation] = Player:new(nation, self)
        local base = self.players[nation]:createBase()
        table.insert(self.entities[nation], base)
    end

    self.background = love.graphics.newImage('resource/Scenario.png')

    self:createButtons()
end

function GameScene:createButtons()
    local buttons = {AssaultUnit, MachineGunUnit, PistolUnit, RocketLauncherUnit,
        SniperRifleUnit, ShotgunUnit}
    local x, y, p = 0, 436, 10

    for _, template in ipairs(buttons) do
        local button = Button:new(
            "American", template,
            x + p, y + p,
            template.sprites[States.IDLE][1],
            function()
                local soldier = self.players["American"]:buy(template)
                table.insert(self.entities["American"], soldier)
            end
        )
        table.insert(self.entities.Buttons, button)
        x = x + button.size.x + (p * 2)
    end

    buttons = {AssaultUnit, MachineGunUnit, PistolUnit, RocketLauncherUnit}
    x, y, p = 735, 510, 10
    for _, template in ipairs(buttons) do
        local button = Button:new(
            "Nazis", template,
            x - p, y + p,
            template.sprites[States.IDLE][1],
            function()
                local soldier = self.players["Nazis"]:buy(template)
                table.insert(self.entities["Nazis"], soldier)
            end
        )
        table.insert(self.entities.Buttons, button)
        x = x - (button.size.x + (p * 2))
    end
end

function GameScene:update(dt)
    self.dt = dt
    for _, units in pairs(self.entities) do
        for _, unit in ipairs(units) do
            unit:update(dt)
        end
    end

    if self.win then
        self.winDt = self.winDt + dt
        if self.winDt > 2 then
            SceneManager.push("Win")
        end
    end
end

function GameScene:draw()
    love.graphics.setColor(255, 255, 255)
    love.graphics.draw(self.background, 0, 0)

    for _, units in pairs(self.entities) do
        for _, unit in ipairs(units) do
            unit:draw()
        end
    end

    for nation, player in pairs(self.players) do
        love.graphics.setFont()
        love.graphics.print(nation .. ": $" .. player.money, unpack(Nations[nation].money))
    end
end

function GameScene:keypressed(key, unicode)
    if key == "p" then
        SceneManager.push("Pause")
    end
end

function GameScene:mousepressed(x, y, button)
    for _, button in ipairs(self.entities.Buttons) do
        if button:inCoordinates(x, y) then
            button.callback()
        end
    end
end

function GameScene:getOppositeNation(nation)
    local nations = table.keys(Nations)
    if nation == nations[1] then
        return nations[2]
    else
        return nations[1]
    end
end

function GameScene:entityDied(entity)
    local nation = self:getOppositeNation(entity.nation)
    if entity.origin.imageName == "Base.png" then
        self.win = true
    end
    local cost = math.floor(entity.origin.cost * 0.6)
    self.players[nation].money = self.players[nation].money + cost
end

function GameScene:createBullet(owner, dPosition)
    local b = Bullet.new(self, owner, dPosition);
    table.insert(self.entities["Bullets"], b);

    return b;
end
